<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item style="font-weight:bolder;">轮播图管理</el-breadcrumb-item>
      <el-breadcrumb-item>轮播图添加</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin-top:20px;">
        <!-- 轮播图添加，图片嵌在表单中上传 -->
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" style="width: 60%; margin-left: 15%;">
          <el-form-item label="轮播图名称" prop="name">
            <el-input v-model="ruleForm.name"  clearable></el-input>
          </el-form-item>
          <el-form-item label="轮播图简介" prop="desc">
            <el-input type="textarea" v-model="ruleForm.desc" ></el-input>
          </el-form-item>
          <el-form-item label="上传图片" ref="uploadElement" prop="imageUrl">
            <!-- 把上传图片的地址放入input，然后上传 -->
            <el-input v-model="ruleForm.url" v-if="false"></el-input>
            <!-- 图片上传 -->
            <el-upload class="avatar-uploader" ref="upload" :show-file-list="false" action="/ordering/pic/upload"
              :before-upload="beforeUpload" :on-change="handleChange" :data="ruleForm" :on-success="afterSuccessUpload">
              <img v-if="ruleForm.imageUrl" :src="ruleForm.imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm()" style="margin-right: 20px;">upLoad</el-button>
            <el-button @click="resetForm()">Reset</el-button>
          </el-form-item>
        </el-form>
    </el-card>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        ruleForm: {
          name: '',
          desc: '',
          imageUrl: '',
          url: ''
        },
        rules: {
          name: [{
            required: true,
            message: '请输入轮播图名称',
            trigger: 'blur'
          }, {
            required: true,
            min: 2,
            max: 8,
            message: '轮播图名称长度至少2个字符，最多8个字符',
            trigger: 'blur'
          }],
          imageUrl: [{
            required: true,
            message: '请上传图片',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      // 提交轮播图表单信息
      submitForm() {
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            const pic = await this.$http.post('/ordering/pic', this.ruleForm)
            if (pic.status !== 200) {
              this.$message.error('轮播图添加失败');
            }
            this.$message.success('轮播图添加成功');
            this.resetForm()
          } else {
            return false;
          }
        })
      },
      // 重置轮播图信息
      resetForm() {
        this.$refs.ruleForm.resetFields();
        this.ruleForm.imageUrl = '';
      },
      // 上传图片的路径改变
      handleChange(file, fileList) {
        this.ruleForm.imageUrl = URL.createObjectURL(file.raw);
      },
      // 上传图片时触发函数
      beforeUpload(file) {
        const isJPG = file.type === 'image/jpeg';

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        return isJPG;
      },
      // 图片上传成功后
      afterSuccessUpload(file) {
        // 上传成功后，后台会返回前台一个图片链接，把这个链接后续写入数据库
        this.ruleForm.url = file.filename
      }
    }
  }
</script>

<style scoped>
  .el-button+.el-button {
    margin-left: 0 !important;
  }

  input[type="file"] {
    display: none;
  }

  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 178px;
    height: 178px;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
